package com.example.myapplication.Services;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import com.example.myapplication.R;

public class LocalServicesActivity extends AppCompatActivity {
  private int serviceCount=0;
  private MyServices.MyBinder binder;
  TextView tipText;

  private boolean isServiceBound=false;
  //创建ServiceConnection对象
  private ServiceConnection serviceConnection=new ServiceConnection() {
    @Override public void onServiceConnected(ComponentName name, IBinder service) {
      binder=(MyServices.MyBinder) service;
      isServiceBound = true;

    }

    @Override public void onServiceDisconnected(ComponentName name) {

      isServiceBound = false;

    }
  };;



  @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.services_local_layout);
    Toolbar toolbar = findViewById(R.id.toolbar);
    toolbar.setTitle("LocalServices实验");
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    Init();

  }

  private void Init() {
    Button bindButton = findViewById(R.id.bind_button);
    Button unbindButton = findViewById(R.id.unbind_button);
    tipText = findViewById(R.id.info_tip);
    tipText.setVisibility(View.INVISIBLE);
    Button tipButton = findViewById(R.id.info_button);

    bindButton.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        BindServices();
        serviceCount++;
      }
    });
    unbindButton.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        if (serviceCount <= 0) {
          return;
        }
        UnBindServices();
        serviceCount--;
      }
    });
    tipButton.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        ShowInfo();
      }
    });
  }

  private void ShowInfo() {
    String str = getString(R.string.services_countInfo, String.valueOf(serviceCount));
    tipText.setText(str);
    tipText.setVisibility(View.VISIBLE);

  }

  private void UnBindServices() {
    if (serviceCount <= 0) {
      return;
    }
    unbindService(serviceConnection);

  }

  private void BindServices() {
    Intent intent = new Intent(this, MyServices.class);
    bindService(intent, serviceConnection, Service.BIND_AUTO_CREATE);

    Toast.makeText(this, "绑定成功", Toast.LENGTH_SHORT).show();

  }



  public void unbindingServices(View view) {
    if (serviceCount <= 0) {
      return;
    }
    unbindService(serviceConnection);
    serviceCount--;
  }

  @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        GoBack();
    }
    return true;
  }

  private void GoBack() {
    finish();
  }
}
